package auth

import (
	"context"
	"gitee.com/libwiki/go-zero-lib/app/user/internal/svc"
	"gitee.com/libwiki/go-zero-lib/app/user/internal/types"
	"gitee.com/libwiki/go-zero-lib/package/jwtx"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}
func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.LoginResponse, err error) {
	expire := l.svcCtx.Config.JwtAuth.AccessExpire
	token, err := jwtx.GetJwtToken(
		l.svcCtx.Config.JwtAuth.AccessSecret,
		expire,
		map[string]any{"userId": 1},
	)
	if err == nil {
		resp = &types.LoginResponse{
			UserInfo: &types.UserInfo{
				Code:      "123123",
				Nickname:  "用户1",
				RealName:  "",
				Avatar:    "",
				Phone:     req.Phone,
				PhoneArea: 0,
			},
			Token:  token.Token,
			Expire: token.Expire,
		}
	}
	return
}
